package com.hong;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Test004 {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //1.new方式创建对象
        User user = new User();
        //2.使用java的反射机制创建对象Class.forName("类的完整路径");
        Class<?> forName = Class.forName("com.hong.User");
        //使用默认无参构造函数创建对象forName.newInstance();使用java的反射机制创建对象
    /*    User classUser = (User) forName.newInstance();
        classUser.setId("22");
        System.out.println(classUser);*/
        //创建带有参的构造函数
        Constructor<?> constructor = forName.getConstructor(String.class,String.class);
        User classUser = (User)constructor.newInstance("12","1111");
        System.out.println(classUser.toString());
    }
}
